﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris
{
    class Figure
    {
        public Node Node1 { get; set; }
        public Node Node2 { get; set; }
        public Node Node3 { get; set; }
        public Node Node4 { get; set; }
        public FigureType figureType;
        public FigurePosition figurePosition = FigurePosition.First;
        private Random rndType = new Random();

        public Figure()
        {
            ChooseFigure(rndType.Next(1, 8));
            Node1 = new Node(ChooseColorForFigure());
            Node2 = new Node(ChooseColorForFigure());
            Node3 = new Node(ChooseColorForFigure());
            Node4 = new Node(ChooseColorForFigure());
        }

        private FigureType ChooseFigure(int figureNumber)
        {
            if (figureNumber == 1)
                figureType = FigureType.Line;
            if (figureNumber == 2)
                figureType = FigureType.Square;
            if (figureNumber == 3)
                figureType = FigureType.ZLeft;
            if (figureNumber == 4)
                figureType = FigureType.ZRight;
            if (figureNumber == 5)
                figureType = FigureType.Piramyd;
            if (figureNumber == 6)
                figureType = FigureType.LLeft;
            if (figureNumber == 7)
                figureType = FigureType.LRight;
            return figureType;
        }

        public ConsoleColor ChooseColorForFigure()
        {
            if (figureType == FigureType.Line)
                return ConsoleColor.Blue;
            if (figureType == FigureType.LLeft)
                return ConsoleColor.DarkMagenta;
            if (figureType == FigureType.LRight)
                return ConsoleColor.Yellow;
            if (figureType == FigureType.Piramyd)
                return ConsoleColor.Green;
            if (figureType == FigureType.ZLeft)
                return ConsoleColor.Cyan;
            if (figureType == FigureType.ZRight)
                return ConsoleColor.DarkCyan;
            return ConsoleColor.Red;
        }
        
        public void MoveDown()
        {
            Node1.MoveDown();
            Node2.MoveDown();
            Node3.MoveDown();
            Node4.MoveDown();
        }

        public void MoveRight()
        {
            Node1.MoveRight();
            Node2.MoveRight();
            Node3.MoveRight();
            Node4.MoveRight();
        }

        public void MoveLeft()
        {
            Node1.MoveLeft();
            Node2.MoveLeft();
            Node3.MoveLeft();
            Node4.MoveLeft();
        }
        
        public void Rotate()
        {
            if (figureType == FigureType.Line)
                RotateLine();
            if (figureType == FigureType.LLeft)
                RotateLLeft();
            if (figureType == FigureType.LRight)
                RotateLRight();
            if (figureType == FigureType.Piramyd)
                RotatePiramyd();
            if (figureType == FigureType.ZLeft)
                RotateZLeft();
            if (figureType == FigureType.ZRight)
                RotateZRight();
        }

        private void RotateLine()
        {
            if (figurePosition == FigurePosition.First)
            {
                Node1.Y++;
                Node1.X++;
                Node3.Y--;
                Node3.X--;
                Node4.Y = Node4.Y - 2;
                Node4.X = Node4.X - 2;
                figurePosition = FigurePosition.Second;
            }
            else
            {
                Node1.Y--;
                Node1.X--;
                Node3.Y++;
                Node3.X++;
                Node4.Y = Node4.Y + 2;
                Node4.X = Node4.X + 2;
                figurePosition = FigurePosition.First;
            }
        }

        private void RotateZRight()
        {
            if (figurePosition == FigurePosition.First)
            {
                Node1.Y--;
                Node1.X--;
                Node3.Y--;
                Node3.X++;
                Node4.X = Node4.X + 2;
                figurePosition = FigurePosition.Second;
            }
            else
            {
                Node1.Y++;
                Node1.X++;
                Node3.Y++;
                Node3.X--;
                Node4.X = Node4.X - 2;
                figurePosition = FigurePosition.First;
            }
        }

        private void RotateZLeft()
        {
            if (figurePosition == FigurePosition.First)
            {
                Node1.Y--;
                Node1.X++;
                Node3.Y--;
                Node3.X--;
                Node4.X = Node4.X - 2;
                figurePosition = FigurePosition.Second;
            }
            else
            {
                Node1.Y++;
                Node1.X--;
                Node3.Y++;
                Node3.X++;
                Node4.X = Node4.X + 2;
                figurePosition = FigurePosition.First;
            }
        }

        private void RotateLLeft()
        {
            if (figurePosition == FigurePosition.First)
            {
                Node1.Y--;
                Node1.X++;
                Node3.Y++;
                Node3.X++;
                Node4.X = Node4.X + 2;
                Node4.Y = Node4.Y + 2;
                figurePosition = FigurePosition.Second;
            }
            else if (figurePosition == FigurePosition.Second)
            {
                Node1.Y++;
                Node1.X++;
                Node3.Y++;
                Node3.X--;
                Node4.Y = Node4.Y + 2;
                Node4.X = Node4.X - 2;
                figurePosition = FigurePosition.Third;
            }
            else if (figurePosition == FigurePosition.Third)
            {
                Node1.Y++;
                Node1.X--;
                Node3.Y--;
                Node3.X--;
                Node4.Y = Node4.Y - 2;
                Node4.X = Node4.X - 2;
                figurePosition = FigurePosition.Fourth;
            }
            else
            {
                Node1.Y--;
                Node1.X--;
                Node3.Y--;
                Node3.X++;
                Node4.Y = Node4.Y - 2;
                Node4.X = Node4.X + 2;
                figurePosition = FigurePosition.First;
            }
        }

        private void RotateLRight()
        {
            if (figurePosition == FigurePosition.First)
            {
                Node1.Y--;
                Node1.X--;
                Node3.Y++;
                Node3.X--;
                Node4.X = Node4.X - 2;
                Node4.Y = Node4.Y + 2;
                figurePosition = FigurePosition.Second;
            }
            else if (figurePosition == FigurePosition.Second)
            {
                Node1.Y++;
                Node1.X--;
                Node3.Y++;
                Node3.X++;
                Node4.Y = Node4.Y + 2;
                Node4.X = Node4.X + 2;
                figurePosition = FigurePosition.Third;
            }
            else if (figurePosition == FigurePosition.Third)
            {
                Node1.Y++;
                Node1.X++;
                Node3.Y--;
                Node3.X++;
                Node4.Y = Node4.Y - 2;
                Node4.X = Node4.X + 2;
                figurePosition = FigurePosition.Fourth;
            }
            else
            {
                Node1.Y--;
                Node1.X++;
                Node3.Y--;
                Node3.X--;
                Node4.Y = Node4.Y - 2;
                Node4.X = Node4.X - 2;
                figurePosition = FigurePosition.First;
            }
        }

        private void RotatePiramyd()
        {
            if (figurePosition == FigurePosition.First)
            {
                Node1.Y++;
                Node1.X--;
                Node3.Y--;
                Node3.X--;
                Node4.X++;
                Node4.Y++;
                figurePosition = FigurePosition.Second;
            }
            else if (figurePosition == FigurePosition.Second)
            {
                Node1.Y++;
                Node1.X++;
                Node3.Y++;
                Node3.X--;
                Node4.Y--;
                Node4.X++;
                figurePosition = FigurePosition.Third;
            }
            else if (figurePosition == FigurePosition.Third)
            {
                Node1.Y--;
                Node1.X++;
                Node3.Y++;
                Node3.X++;
                Node4.Y--;
                Node4.X--;
                figurePosition = FigurePosition.Fourth;
            }
            else
            {
                Node1.Y--;
                Node1.X--;
                Node3.Y--;
                Node3.X++;
                Node4.Y++;
                Node4.X--;
                figurePosition = FigurePosition.First;
            }
        }

        public void CreateLine(int controlX, int controlY)
        {
            Node1.X = controlX;
            Node1.Y = controlY -1;
            Node2.X = controlX;
            Node2.Y = controlY;
            Node3.X = controlX;
            Node3.Y = controlY + 1;
            Node4.X = controlX;
            Node4.Y = controlY + 2;
        }

        public void CreateSquare(int controlX, int controlY)
        {
            Node1.X = controlX - 1;
            Node1.Y = controlY;
            Node2.X = controlX;
            Node2.Y = controlY;
            Node3.X = controlX - 1;
            Node3.Y = controlY + 1;
            Node4.X = controlX;
            Node4.Y = controlY + 1;
        }

        public void CreatePyramid(int controlX, int controlY)
        {
            Node1.X = controlX;
            Node1.Y = controlY - 1;
            Node2.X = controlX;
            Node2.Y = controlY;
            Node3.X = controlX + 1;
            Node3.Y = controlY;
            Node4.X = controlX - 1;
            Node4.Y = controlY;
        }

        public void CreateLleft(int controlX, int controlY)
        {
            Node1.X = controlX - 1;
            Node1.Y = controlY;
            Node2.X = controlX;
            Node2.Y = controlY;
            Node3.X = controlX;
            Node3.Y = controlY - 1;
            Node4.X = controlX;
            Node4.Y = controlY - 2;
        }

        public void CreateLright(int controlX, int controlY)
        {
            Node1.X = controlX + 1;
            Node1.Y = controlY;
            Node2.X = controlX;
            Node2.Y = controlY;
            Node3.X = controlX;
            Node3.Y = controlY - 1;
            Node4.X = controlX;
            Node4.Y = controlY - 2;
        }

        public void CreateZleft(int controlX, int controlY)
        {
            Node1.X = controlX - 1;
            Node1.Y = controlY;
            Node2.X = controlX;
            Node2.Y = controlY;
            Node3.X = controlX;
            Node3.Y = controlY + 1;
            Node4.X = controlX + 1;
            Node4.Y = controlY + 1;
        }

        public void CreateZright(int controlX, int controlY)
        {
            Node1.X = controlX + 1;
            Node1.Y = controlY;
            Node2.X = controlX;
            Node2.Y = controlY;
            Node3.X = controlX;
            Node3.Y = controlY + 1;
            Node4.X = controlX - 1;
            Node4.Y = controlY + 1;
        }

    }
    public enum FigureType
    {
        Line,
        Square,
        ZRight,
        ZLeft,
        LRight,
        LLeft,
        Piramyd
    }

    public enum FigurePosition
    {
        First,
        Second,
        Third,
        Fourth
    }
}
